﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biztalk_Alerts.Models;
using System.Data;
using Biztalk_Alerts.DBOps;

namespace Biztalk_Alerts
{
    public partial class Alerts : System.Web.UI.Page
    {
        IList<ReceiveLocations> rcvLocations = null;
        ReceiveLocations singleLocation = null; 
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                
                //connect and fetch Application Name
                IList<Applications> apps = DBOps.BTOPerations.getApplications();
                grdApplication.DataSource = apps;
                grdApplication.DataBind();
               
            }
        }

 
        private void LoadReceiveLocations()
        {
            if (null == rcvLocations)
                rcvLocations = DBOps.BTOPerations.fetchReceiveLocations(appName);
        }

       
        protected void ddlReceivePortName_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadReceiveLocations();         

            GrdLocationsDetails.DataSource = rcvLocations;
            GrdLocationsDetails.DataBind();
        }

        
        protected void LocationsDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                singleLocation = (ReceiveLocations)e.Row.DataItem;
            }
        }
        string appName = string.Empty;
        protected void grdApplication_SelectedIndexChanged(object sender, EventArgs e)
        {

            appName  = grdApplication.SelectedRow.Cells[0].Text;            

            LoadReceiveLocations();

            GrdLocationsDetails.DataSource = rcvLocations;
            GrdLocationsDetails.DataBind();
        }

        protected void grdApplication_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
                e.Row.ToolTip = "Click last column for selecting this row.";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            IList<LocationSetup> locSetUpList = new  List<LocationSetup>();
            string applicationName = grdApplication.SelectedRow.Cells[0].Text.ToString();
            string receiveLocationName = GrdLocationsDetails.SelectedRow.Cells[0].Text.ToString();

            if (applicationName != string.Empty && receiveLocationName != string.Empty)
            {
                locSetUpList.Add(new LocationSetup()
                {
                    AppName = applicationName
                    ,
                    mailDuration = 5
                    ,
                    others = txtEmail1.Text.ToString().Trim()
                    ,
                    Secondary = txtEmail2.Text.ToString().Trim()
                    ,
                    Primary = txtEmail1.Text.ToString().Trim()
                    ,
                    email = chkEmail.Checked
                    ,
                    restartIfStopped = chkRestartIfStopped.Checked
                    ,
                    ReceiveLocationName = receiveLocationName
                });
                BTAlertOperations.InsertLocationSetup(locSetUpList);          
            }
            else
            {

            }
            
        }

        protected void GrdSendLocationDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

       
    }
}